<?php

namespace App\Http\Controllers\Study;

use App\Repositories\StudyRepository;
use Exception;
use App\Models\Study;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Exceptions\GeneralException;
use App\Http\Requests\Logic\Study\RecordRequest;
use App\Http\Requests\Logic\Study\StudyReadRequest;
use App\Http\Requests\Logic\Study\StudyShowRequest;
use App\Http\Requests\Logic\Study\StudyStoreReqeust;
use App\Http\Requests\Logic\Study\StudyIndexRequest;
use App\Http\Requests\Logic\Study\StudyCountRequest;
use App\Http\Requests\Logic\Study\StudyUpdateRequest;
use App\Http\Requests\Logic\Study\CountDetailRequest;
use App\Http\Requests\Logic\Study\StudyDestroyRequest;




class StudyController extends Controller
{

    public $studyRepository;

    public function __construct(StudyRepository $studyRepository)
    {
        $this->studyRepository = $studyRepository;
    }

    /**
     * 获取培训资料列表
     *
     * Date: 2018/5/29
     * @author Sam
     * @param StudyIndexRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(StudyIndexRequest $request)
    {
        $type = $request->get('type');
        $list = Study::query()->where('type', $type)->get()->map(function ($item) {
            $item->content = json_decode($item->content);
            return $item;
        });
        return success($list);
    }


    /**
     * 创建培训
     *
     * Date: 2018/5/29
     * @author Sam
     * @param StudyStoreReqeust $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(StudyStoreReqeust $request)
    {
        $params = [
            1 => [
                'name', 'gender', 'age', 'height', 'position', 'work_time_begin', 'work_time_end', 'plate_number', 'mobile', 'desc', 'pic'
            ],
            2 => [
                'title', 'content', 'pic'
            ],
        ];
        $serialize = $request->only($params[$request->get('type')]);
        $attributes['content'] = json_encode($serialize);
        $attributes['type'] = $request->get('type');
        $attributes['project_id'] = $request->get('project_id');
        $res = Study::query()->create($attributes);
        $res['content'] = json_decode($res['content']);
        return success($res);
    }

    /**
     * 获取单个资料信息
     *
     * Date: 2018/5/29
     * @author Sam
     * @param StudyShowRequest $request
     * @param Study $study
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(StudyShowRequest $request, Study $study)
    {
        $study->content = json_decode($study->content);
        return success($study);
    }

    /**
     * 修改培训资料
     *
     * Date: 2018/5/29
     * @author Sam
     * @param StudyUpdateRequest $request
     * @param Study $study
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(StudyUpdateRequest $request, Study $study)
    {
        $params = [
            1 => [
                'name', 'gender', 'age', 'height', 'position', 'work_time_begin', 'work_time_end', 'plate_number', 'mobile', 'desc', 'pic'
            ],
            2 => [
                'title', 'content', 'pic'
            ],
        ];
        $serialize = $request->only($params[$request->get('type')]);
        $attributes['content'] = json_encode($serialize);
        foreach ($attributes as $k => $v) {
            $study->$k = $v;
        }
        $study->update();
        $study->content = json_decode($study->content);
        return updated($study);
    }

    /**
     * 删除培训资料
     *
     * Date: 2018/5/29
     * @author Sam
     * @param StudyDestroyRequest $request
     * @param Study $study
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(StudyDestroyRequest $request, Study $study)
    {
        $study->delete();
        return deleted();
    }


    public function record(RecordRequest $request, Study $study)
    {
        $user = $request->user();
        $res = DB::table('user_study_pivot')->where(['study_id' => $study->id, 'user_id' => $user->id, 'project_id' => $study->project_id])->get();

        if ($res->isEmpty()) {
            try {
                DB::table('user_study_pivot')->insert(['study_id' => $study->id, 'user_id' => $user->id, 'project_id' => $study->project_id, 'type' => $study->type]);
            } catch (Exception $e) {
                throw new GeneralException('未知错误');
            }
        }
        return message('success');
    }

    /**
     * 获取当前用户的培训进度
     *
     * Date: 2018/5/30
     * @author Sam
     * @param StudyReadRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function read(StudyReadRequest $request)
    {
        $user = $request->user();
        $list = $this->studyRepository->read($user);
        return success($list);
    }


    /**
     * 各项目两种培训资料完成统计
     *
     * Date: 2018/5/30
     * @author Sam
     * @param StudyCountRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function StudyCount(StudyCountRequest $request)
    {
        $paginate = $request->get('paginate',10);
        $project_id = $request->get('project_id',false);
        $company_id = Auth::user()->company_id;
        $list = $this->studyRepository->stydyCount($paginate,$project_id,$company_id);
        return success($list);
    }

    /**
     * 单独项目两种培训资料统计
     *
     * Date: 2018/5/30
     * @author Sam
     * @param CountDetailRequest $request
     * @param $project
     * @return \Illuminate\Http\JsonResponse
     */
    public function CountDetail(CountDetailRequest $request,$project)
    {
        $paginate = $request->get('paginate',10);
        $user_id = $request->get('user_id',false);
        $list = $this->studyRepository->countDetail($project,$paginate,$user_id);
        return success($list);
    }
}
